﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using THEDARKKNIGHT.UI;
using System;
using UnityEngine.UI;
using THEDARKKNIGHT.EventSystem;
using UnityEngine.EventSystems;
using UnityEngine.Events;

public class PlayWindows : BBaseWindows
{

    private GameObject ReUIRoot;
    private RawImage PlayBtnIcorn;
    private Button PlayBtn;

    private Texture2D PlayIcorn;

    private Texture2D PauseIcorn;

    public Slider ProgressSlider { get; private set; }
    public Text CurrentTime { get; private set; }
    public Text CountTime { get; private set; }
    public RawImage PlayScreen { get; private set; }

    public EventTrigger SliderTrigger;

    public bool ProgressSliderCtrlLock = false;

    public enum PLAYSTATUS {
        NONE,
        PLAY,
        PAUSE,
    }

    public PLAYSTATUS CurrentStatus;

    public float TimeTick = 0;

    public int TickCount = 0;

    public PlayWindows(string windowsName)
    {
        this.WindowsAlias = windowsName;
    }

    public override void AddListener()
    {
        PlayBtn.onClick.AddListener(PlayBtnOnClickCallback);
        BEventManager<object>.Instance().AddListener(UIEventDefine.VIDEOTIMECHANGE, VideoTimeChangeCallback);
        BEventManager<object>.Instance().AddListener(UIEventDefine.READYTOPLAY, VideoReadyToPlayCallback);
        BEventManager<object>.Instance().AddListener(UIEventDefine.VIDEOPLAYFINISH, VideoPlayFinishCallback);
        AddSliderListener(EventTriggerType.Drag,SliderOnDragEventCallback);
        AddSliderListener(EventTriggerType.PointerUp, SliderPointUpEventCallback);
        AddSliderListener(EventTriggerType.PointerDown, SliderPointerDownEventCallback);
    }

    private object VideoPlayFinishCallback(object data)
    {
        CurrentStatus = PLAYSTATUS.NONE;
        PlayScreen.gameObject.SetActive(false);
        PlayBtnIcorn.texture = PlayIcorn;
        CurrentTime.text = SetPlayTime(0);
        return null;
    }

    private void SliderPointerDownEventCallback(BaseEventData data)
    {
        ProgressSliderCtrlLock = true;
    }

    private void SliderPointUpEventCallback(BaseEventData data)
    {
        BEventManager<object>.Instance().DispatchEvent(UIEventDefine.SEEKVIDEOTIME, ProgressSlider.value);
        TickCount = 2;//马上刷新进度条
        ProgressSliderCtrlLock = false;
    }

    private void SliderOnDragEventCallback(BaseEventData data)
    {
        ProgressSliderCtrlLock = true;
    }

    private void AddSliderListener(EventTriggerType type,UnityAction<BaseEventData> callback)
    {
        EventTrigger.Entry eventItem = new EventTrigger.Entry();
        eventItem.eventID = type;
        eventItem.callback.AddListener(callback);
        SliderTrigger.triggers.Add(eventItem);
    }

    private void RemoveAllSliderListener() {
        SliderTrigger.triggers.Clear();
    }

    private object VideoReadyToPlayCallback(object data)
    {
        if ( data != null) {
            int time = ((VideoStatus)data).SumPlayTime;
            CurrentTime.text = SetPlayTime(0);
            CountTime.text   = SetPlayTime(time);
        }
        return null;
    }

    private object VideoTimeChangeCallback(object data)
    {
        if (data != null ) {
            int time = ((VideoStatus)data).CurrentTime;
            CurrentTime.text = SetPlayTime(time);
        }
        return null;
    }

    private string SetPlayTime(int time)
    {
        int hour = time / 3600;
        int min  = (int)(time - hour * 3600) / 60;
        int sec  = (int)(time - hour * 3600 - min * 60);
        string currentHour = hour >= 10  ? hour.ToString() : "0" + hour;
        string currentMin  = min  >= 10  ? min.ToString()  : "0" + min;
        string currentSec  = sec  >= 10  ? sec.ToString() : "0" + sec;
        return currentHour + " : " + currentMin + ":" + currentSec;
    }

    public override void DataInit()
    {
        CurrentStatus = PLAYSTATUS.NONE;
        ReUIRoot = Resources.Load<GameObject>("UIPrefabs/PlayCtrlPanel");
        PauseIcorn = Resources.Load<Texture2D>("Texture/Pause");
        PlayIcorn = Resources.Load<Texture2D>("Texture/Play");
        SetLifeCycleType(THEDARKKNIGHT.LifeCycleTool.LifeType.Update,true);
    }

    public override void DestoryWindows()
    {
        ReUIRoot = null;
    }

    public override bool GetWindowsMsg(int windowsID, string windowsAlias, object data)
    {
        switch (windowsAlias)
        {
            case "PlayWindows":
                PlayBtnIcorn.texture = PauseIcorn;
                CurrentStatus = PLAYSTATUS.PLAY;
                PlayScreen.gameObject.SetActive(true);
                BEventManager<object>.Instance().DispatchEvent(UIEventDefine.STARTTOPLAY, data);
                break;
        }
        return true;
    }

    public override void Hide()
    {

    }

    public override void RemoveListener()
    {
        PlayBtn.onClick.RemoveListener(PlayBtnOnClickCallback);
        BEventManager<object>.Instance().RemoveListener(UIEventDefine.VIDEOTIMECHANGE, VideoTimeChangeCallback);
        BEventManager<object>.Instance().RemoveListener(UIEventDefine.READYTOPLAY, VideoReadyToPlayCallback);
        BEventManager<object>.Instance().RemoveListener(UIEventDefine.VIDEOPLAYFINISH, VideoPlayFinishCallback);
        RemoveAllSliderListener();
    }

    public override void Show()
    {

    }

    public override Transform UIInit(Transform UIRoot)
    {
        GameObject playUIRoot = GameObject.Instantiate(ReUIRoot, UIRoot);
        Transform PlayGB = playUIRoot.transform.Find("PlayCtrl/PlayBtn");
        Transform ProgressSliderGB = playUIRoot.transform.Find("PlayCtrl/ProgressSlider");

        CurrentTime = playUIRoot.transform.Find("PlayCtrl/Time/CurrentTime").GetComponent<Text>();
        CountTime = playUIRoot.transform.Find("PlayCtrl/Time/TotalTime").GetComponent<Text>();
        PlayScreen = playUIRoot.transform.Find("PlayScreen/PlayRendtexture").GetComponent<RawImage>();


        SliderTrigger = ProgressSliderGB.GetComponent<EventTrigger>();
        ProgressSlider = ProgressSliderGB.GetComponent<Slider>();
        PlayBtnIcorn = PlayGB.GetComponent<RawImage>();
        PlayBtn = PlayGB.GetComponent<Button>();

        return playUIRoot.transform;
    }

    private void PlayBtnOnClickCallback()
    {
        switch (CurrentStatus) {
            case PLAYSTATUS.PAUSE:
                PlayVideoFunction();
                break;
            case PLAYSTATUS.PLAY:
                PauseVideoFunction();
                break;
        }
    }

    private void PauseVideoFunction()
    {
        VideoStatus videoData = new VideoStatus();
        videoData.IsPlay = false;
        BEventManager<object>.Instance().DispatchEvent(UIEventDefine.PAUSETOPLAY, videoData);
        PlayBtnIcorn.texture = PlayIcorn;
        CurrentStatus = PLAYSTATUS.PAUSE;
    }

    private void PlayVideoFunction()
    {
        VideoStatus videoData = new VideoStatus();
        videoData.IsPlay = true;
        BEventManager<object>.Instance().DispatchEvent(UIEventDefine.STARTTOPLAY, videoData);
        PlayBtnIcorn.texture = PauseIcorn;
        CurrentStatus = PLAYSTATUS.PLAY;
    }

    public override void Update(MonoBehaviour main)
    {
        if (CurrentStatus == PLAYSTATUS.PLAY) {
            if (TimeTick >= 1)
            {
                if (!ProgressSliderCtrlLock && TickCount>=2) {

                    BEventManager<object>.Instance().DispatchEvent(UIEventDefine.VIDEOPLAYSLIDERVALUE, null, (object result) => {
                        ProgressSlider.value = (float)result;
                    });
                    TickCount = 0;
                }
                BEventManager<object>.Instance().DispatchEvent(UIEventDefine.VIDEOPLAYTIME, null, (object result) => {
                    CurrentTime.text = SetPlayTime((int)result);
                });
                TimeTick = 0;
                TickCount++;
            }
            else
            {
                TimeTick += Time.deltaTime;
            }

        }
    }
}
